//自实现string 字符串类      
//构造、析构、拷贝构造、重载运算符+ = == > <  []支持下标访问与修改 、返回字符串指针 c_str();

#include<iostream>
using namespace std;
#include<string.h>
#include<string>

class myString
{
public:
    myString(const char *s = NULL) //默认参数构造器
    {
        if(s == NULL)
        {
            str = new char[1];
            *str = '\0';
            //当 String s; 时 未初始化字符串内容的情况
        }
        else
        {
            int len = strlen(s);
            str = new char[len+1];
            strcpy(str,s);
        }
    }
    ~myString()
    {
        delete []str;
    }
    
    myString(const myString &another) //自实现拷贝构造器
    {
        // str = another.str; //浅拷贝  原对象与拷贝对象指向同一段空间。导致重析构
        //深拷贝，拷贝对象也拥有自己的空间了。
        int len = strlen(another.str); 
        str = new char[len+1];
        strcpy(str,another.str);
    }

    myString* operator= (const myString &another) //重载 = ，实现字符串的赋值。
    {
        if(this->str = another.str) //避免 s1 = s1 的自拷贝情况
            return this; 

        int len = strlen(this->str); 
        str = new char[len+1];
        strcpy(this->str,another.str);
        return this;
    }

    myString operator+(const myString &another) //重载 + 将字符串连接起来
    {
        myString tmp;
        delete []tmp.str;
        int len = strlen(this->str);
        len += strlen(another.str); //求出二者字符串长度之和
        tmp.str = new char[len+1];
        strcat(tmp.str,this->str);
        strcat(tmp.str,another.str);
        return tmp;
    }

    myString operator+(myString &another) //重载 + 将字符串连接起来
    {
        myString tmp;
        delete []tmp.str;
        int len = strlen(this->str);
        len += strlen(another.str); //求出二者字符串长度之和
        tmp.str = new char[len+1];
        strcat(tmp.str,this->str);
        strcat(tmp.str,another.str);
        return tmp;
    }
    bool operator > (const myString &another)
    {
        return (strcmp(this->str,another.str)>0);
    }
    bool operator == (const myString &another)
    {
        return (strcmp(this->str,another.str) == 0);
    }
    bool operator < (const myString &another)
    {
        return (strcmp(this->str,another.str)<0);
    }

    char &operator[](int idx) //返回引用 可供下标访问 并修改数据
    {
        return this->str[idx];
    }

    char *c_str()
    {
        return str; //str指向字符串空间的首地址
    }
    void display()
    {
        cout<<str<<endl;
    }

private:
    char *str;
};

int main()
{
    myString s1("taq");
    myString s2("baa");
    // s1 = s1 + s2;
    // s1.display();
    
    if(s1 == s2)
        cout<<" == "<<endl;
    else if(s1 > s2)
        cout<<" > "<<endl;
    else
        cout<<" < "<<endl;
    s1[2] = 'o';
    s1.display();
    return 0;
}